<?php


namespace app\controller\goods;
require_once __DIR__.'/../../common/AuthController.php';
use app\common\AuthController;


/**
 * 商品档案
 */
class Index extends AuthController
{   


    /**
     * 初始化操作
     */
    public function init()
    {

    }
	

    /**
     * 获取商品列表
     */
	public function api_goods_index_getList($msg)
    {   
        global $db;
        $info = [
            'page' =>  isset($msg['data']['page'])?$msg['data']['page']:1,
            'rows' => isset($msg['data']['rows'])?$msg['data']['rows']:15,
            'id' => isset($msg['data']['id'])?$msg['data']['id']:'',
            'name' => isset($msg['data']['name'])?$msg['data']['name']:'',
            'code' => isset($msg['data']['code'])?$msg['data']['code']:'',
            'brand_name' => isset($msg['data']['brand_name'])?$msg['data']['brand_name']:'',
            'category_name_1' => isset($msg['data']['category_name_1'])?$msg['data']['category_name_1']:'',
            'category_name_2' => isset($msg['data']['category_name_2'])?$msg['data']['category_name_2']:'',
            'attribute' => isset($msg['data']['attribute'])?$msg['data']['attribute']:'',
            'supplier_id' => isset($msg['data']['supplier_id'])?$msg['data']['supplier_id']:'',
            'supplier_name' => isset($msg['data']['supplier_name'])?$msg['data']['supplier_name']:'',
            'status' => isset($msg['data']['status'])?$msg['data']['status']:'',
            'is_discount' => isset($msg['data']['is_discount'])?$msg['data']['is_discount']:'',
        ];
        $limit = ($info['page']-1)*$info['rows'].','.$info['rows'];
        $where = "is_delete = 0";
        if(!empty($info['id'])){
            $where .= " AND id =".$info['id'];
        }
        if(!empty($info['status'])){
            $where .= " AND status =".$info['status'];
        }
        if(!empty($info['supplier_id'])){
            $where .= " AND supplier_id =".$info['supplier_id'];
        }
        if(!empty($info['is_discount'])){
            $where .= " AND is_discount =".$info['is_discount'];
        }
        if(!empty($info['name'])){
            $where .= " AND name LIKE '%".$info['name']."%'";
        }
        if(!empty($info['code'])){
            $where .= " AND code LIKE '%".$info['code']."%'";
        }
        if(!empty($info['brand_name'])){
            $where .= " AND brand_name LIKE '%".$info['brand_name']."%'";
        }
        if(!empty($info['category_name_1'])){
            $where .= " AND category_name_1 LIKE '%".$info['category_name_1']."%'";
        }
        if(!empty($info['category_name_2'])){
            $where .= " AND category_name_2 LIKE '%".$info['category_name_2']."%'";
        }
        if(!empty($info['attribute'])){
            $where .= " AND attribute LIKE '%".$info['attribute']."%'";
        }
        if(!empty($info['supplier_name'])){
            $where .= " AND supplier_name LIKE '%".$info['supplier_name']."%'";
        }
        $list = $db->query("select * from goods where ".$where ." order by id  desc limit ".$limit); 
        $count =  $db->query("SELECT count(*) as count from goods WHERE ".$where);
        return $this->outMessage(10000,'获取成功',$list,'success',$count[0]['count']);
	}

    

    /**
     * 添加商品
     */
    public function api_goods_index_add($msg)
    {   
        $info = [
            'name' =>  isset($msg['data']['name'])?$msg['data']['name']:'',
            'code' => isset($msg['data']['code'])?$msg['data']['code']:'',
            'brand_id' => isset($msg['data']['brand_id'])?$msg['data']['brand_id']:'',
            'brand_name' => isset($msg['data']['brand_name'])?$msg['data']['brand_name']:'',
            'category_id_1' =>  isset($msg['data']['category_id_1'])?$msg['data']['category_id_1']:'',
            'category_name_1' => isset($msg['data']['category_name_1'])?$msg['data']['category_name_1']:'',
            'category_id_2' => isset($msg['data']['category_id_2'])?$msg['data']['category_id_2']:'',
            'category_name_2' => isset($msg['data']['category_name_2'])?$msg['data']['category_name_2']:'',
            'units_id' =>  isset($msg['data']['units_id'])?$msg['data']['units_id']:'',
            'units_name' => isset($msg['data']['units_name'])?$msg['data']['units_name']:'',
            'attribute' => isset($msg['data']['attribute'])?$msg['data']['attribute']:'',
            'valid_time' => isset($msg['data']['valid_time'])?$msg['data']['valid_time']:'',
            'supply_time' =>  isset($msg['data']['supply_time'])?$msg['data']['supply_time']:'',
            'buy_price' => isset($msg['data']['buy_price'])?$msg['data']['buy_price']:'',
            'price' => isset($msg['data']['price'])?$msg['data']['price']:'',
            'buy_min_number' => isset($msg['data']['buy_min_number'])?$msg['data']['buy_min_number']:'',
            'supplier_id' => isset($msg['data']['supplier_id'])?$msg['data']['supplier_id']:'',
            'supplier_name' => isset($msg['data']['supplier_name'])?$msg['data']['supplier_name']:'',
            'main_pic' => isset($msg['data']['main_pic'])?$msg['data']['main_pic']:'',
            'is_discount' => isset($msg['data']['is_discount'])?$msg['data']['is_discount']:'',
        ];
        foreach ($info as $v) {
            if(empty($v)){
                return $this->outMessage(-1,'参数不能为空');
            }
        }
	$info['status'] = isset($msg['data']['status'])?$msg['data']['status']:1;
        $info['warm_time'] = isset($msg['data']['warm_time'])?$msg['data']['warm_time']:0;
        $info['threshold_value'] =  isset($msg['data']['threshold_value'])?$msg['data']['threshold_value']:'';
        $info['min_stock_alarm'] = isset($msg['data']['min_stock_alarm'])?$msg['data']['min_stock_alarm']:'';
        $info['stock'] = isset($msg['data']['stock'])?$msg['data']['stock']:0;
        $info['description'] = isset($msg['data']['description'])?$msg['data']['description']:'';
        $info['details_pic'] =  isset($msg['data']['details_pic'])?$msg['data']['details_pic']:'';
        if(!$this->validate->checkName($info['name'])){
            return $this->outMessage(-1,'请输入正确的名称');
        }
        if(!is_numeric($info['code']) || strlen($info['code']) !== 13){
            return $this->outMessage(-1,'请输入正确的商品条码');
        }
        if(!empty($info['description'])){
            if(!$this->validate->checkName($info['description'],2,100)){
                return $this->outMessage(-1,'请输入正确的描述');
            }
        }
        $res = $this->table->find('goods',['code'=>$info['code']]);
        if($res){
            return $this->outMessage(-1,'该商品条码已存在不可重复');
        }
        $data = [
            'name' => $info['name'],
            'code' => $info['code'],
            'brand_id' => $info['brand_id'],
            'brand_name' => $info['brand_name'],
            'category_id_1' => $info['category_id_1'],
            'category_name_1' => $info['category_name_1'],
            'category_id_2' => $info['category_id_2'],
            'category_name_2' => $info['category_name_2'],
            'attribute' => $info['attribute'],
            'units_id' => $info['units_id'],
            'units_name' => $info['units_name'],
            'valid_time' => $info['valid_time'],
            'supply_time' => $info['supply_time'],
            'buy_price' => $info['buy_price'],
            'price' => $info['price'],
            'threshold_value' => $info['threshold_value'],
            'min_stock_alarm' => $info['min_stock_alarm'],
            'buy_min_number' => $info['buy_min_number'],
            'supplier_id' => $info['supplier_id'],
            'supplier_name' => $info['supplier_name'],
            'status' => $info['status'],
            'main_pic' => $info['main_pic'],
            'description' => $info['description'],
            'details_pic' => $info['details_pic'],
            'warm_time' => $info['warm_time'],
            'is_discount' => $info['is_discount'],
            'create_time' => date('Y-m-d H:i:s',time())
        ];
        $res = $this->table->insert('goods',$data);
        if(!$res){
            return $this->outMessage(-1,'添加失败');
        }
        return $this->outMessage(10000,'添加成功');
    }



	/**
	 * 编辑
	 */
	public function api_goods_index_edit($msg)
    {
        $info = [
            'id' =>  isset($msg['data']['id'])?$msg['data']['id']:'',
            'name' =>  isset($msg['data']['name'])?$msg['data']['name']:'',
            'code' => isset($msg['data']['code'])?$msg['data']['code']:'',
            'brand_id' => isset($msg['data']['brand_id'])?$msg['data']['brand_id']:'',
            'brand_name' => isset($msg['data']['brand_name'])?$msg['data']['brand_name']:'',
            'category_id_1' =>  isset($msg['data']['category_id_1'])?$msg['data']['category_id_1']:'',
            'category_name_1' => isset($msg['data']['category_name_1'])?$msg['data']['category_name_1']:'',
            'category_id_2' => isset($msg['data']['category_id_2'])?$msg['data']['category_id_2']:'',
            'category_name_2' => isset($msg['data']['category_name_2'])?$msg['data']['category_name_2']:'',
            'units_id' =>  isset($msg['data']['units_id'])?$msg['data']['units_id']:'',
            'units_name' => isset($msg['data']['units_name'])?$msg['data']['units_name']:'',
            'attribute' => isset($msg['data']['attribute'])?$msg['data']['attribute']:'',
            'valid_time' => isset($msg['data']['valid_time'])?$msg['data']['valid_time']:'',
            'supply_time' =>  isset($msg['data']['supply_time'])?$msg['data']['supply_time']:'',
            'buy_price' => isset($msg['data']['buy_price'])?$msg['data']['buy_price']:'',
            'price' => isset($msg['data']['price'])?$msg['data']['price']:'',
            'buy_min_number' => isset($msg['data']['buy_min_number'])?$msg['data']['buy_min_number']:'',
            'supplier_id' => isset($msg['data']['supplier_id'])?$msg['data']['supplier_id']:'',
            'supplier_name' => isset($msg['data']['supplier_name'])?$msg['data']['supplier_name']:'',
            'main_pic' => isset($msg['data']['main_pic'])?$msg['data']['main_pic']:'',
            'is_discount' => isset($msg['data']['is_discount'])?$msg['data']['is_discount']:'',
        ];
        foreach ($info as $v) {
            if(empty($v)){
                return $this->outMessage(-1,'参数不能为空');
            }
        }
        $info['status'] = isset($msg['data']['status'])?$msg['data']['status']:1;
        $info['warm_time'] = isset($msg['data']['warm_time'])?$msg['data']['warm_time']:0;
        $info['threshold_value'] =  isset($msg['data']['threshold_value'])?$msg['data']['threshold_value']:'';
        $info['min_stock_alarm'] = isset($msg['data']['min_stock_alarm'])?$msg['data']['min_stock_alarm']:'';
        $info['stock'] = isset($msg['data']['stock'])?$msg['data']['stock']:0;
        $info['description'] = isset($msg['data']['description'])?$msg['data']['description']:'';
        $info['details_pic'] =  isset($msg['data']['details_pic'])?$msg['data']['details_pic']:'';
        if(!$this->validate->checkName($info['name'])){
            return $this->outMessage(-1,'请输入正确的名称');
        }
        if(!is_numeric($info['code']) || strlen($info['code']) !== 13){
            return $this->outMessage(-1,'请输入正确的商品条码');
        }
        if(!empty($info['description'])){
            if(!$this->validate->checkName($info['description'],2,100)){
                return $this->outMessage(-1,'请输入正确的描述');
            }
        }
        $res = $this->table->find('goods',['code'=>$info['code']]);
        if(!empty($res) && $res['id'] != $info['id']){
            return $this->outMessage(-1,'该商品条码已存在不可重复');
        }
        $data = [
            'name' => $info['name'],
            'code' => $info['code'],
            'brand_id' => $info['brand_id'],
            'brand_name' => $info['brand_name'],
            'category_id_1' => $info['category_id_1'],
            'category_name_1' => $info['category_name_1'],
            'category_id_2' => $info['category_id_2'],
            'category_name_2' => $info['category_name_2'],
            'attribute' => $info['attribute'],
            'units_id' => $info['units_id'],
            'units_name' => $info['units_name'],
            'valid_time' => $info['valid_time'],
            'supply_time' => $info['supply_time'],
            'buy_price' => $info['buy_price'],
            'price' => $info['price'],
            'threshold_value' => $info['threshold_value'],
            'min_stock_alarm' => $info['min_stock_alarm'],
            'buy_min_number' => $info['buy_min_number'],
            'supplier_id' => $info['supplier_id'],
            'supplier_name' => $info['supplier_name'],
            'status' => $info['status'],
            'main_pic' => $info['main_pic'],
            'description' => $info['description'],
            'details_pic' => $info['details_pic'],
            'warm_time' => $info['warm_time'],
            'is_discount' => $info['is_discount'],
            'update_time' => date('Y-m-d H:i:s',time())
        ];
        $res = $this->table->update('goods',['id'=>$info['id']],$data);
        if(!$res){
            return $this->outMessage(-1,'保存失败');
        }
        return $this->outMessage(10000,'保存成功');
	}



    /**
     * 删除
     */
    public function api_goods_index_delete($msg)
    {
        $id = isset($msg['data']['id'])?$msg['data']['id']:'';
        if(empty($id)){
            return $this->outMessage(-1,'ID不能为空');
        }
        $goods = $this->table->find('goods',['id'=>$id]);
        if(empty($goods)){
            return $this->outMessage(-1,'该商品不存在');
        }
        if($goods['status'] == 2){
            return $this->outMessage(-1,'该商品处于上架状态,不可删除.');
        }
        $res = $this->table->delete('goods',['id'=>$id]);
        if(!$res){
            return $this->outMessage(-1,'删除失败');
        }
        return $this->outMessage(10000,'删除成功');
    }



    /**
     * 上下架
     */
    public function api_goods_index_editStatus($msg)
    {
        $id = isset($msg['data']['id'])?$msg['data']['id']:'';
        $status = isset($msg['data']['status'])?$msg['data']['status']:'';
        if(empty($id)){
            return $this->outMessage(-1,'ID不能为空');
        }
        if($status > 2 && $status < 1 || empty($status) || !is_numeric($status)){
            return $this->outMessage(-1,'状态参数错误');
        }
        $res = $this->table->update('goods',['id'=>$id],['status'=>$status]);
        if(!$res){
            return $this->outMessage(-1,'保存失败');
        }
        return $this->outMessage(10000,'保存成功');
    }



    /**
     * 生成采购单
     */
    public function api_goods_index_createPurchaseRequest($msg)
    {   
        global $db;
        $ids = isset($msg['data']['ids'])?$msg['data']['ids']:'';
        if(empty($ids)){
            return $this->outMessage(-1,'ids不能为空');
        }
        $list = $db->query("SELECT b.id as depot_id,b.name as depot_name,b.linkman,b.linkman_mobile,b.region_name,b.address,a.goods_id,a.goods_name,a.goods_code,a.category_name,a.supplier_id,a.supplier_name,a.attribute,a.units_name,a.brand_name FROM depot_stock as a left join depot as b on a.depot_id = b.id WHERE a.id in ($ids) GROUP BY a.goods_id");
        $arr = [];
        $data = [];
        $depot = [];
        foreach ($list as $k => $v) {
            if(!in_array($v['supplier_id'],$arr)){
                $data[$v['supplier_id']]['supplier_id'] = $v['supplier_id'];
                $data[$v['supplier_id']]['supplier_name'] = $v['supplier_name'];
                // if(!in_array($v['depot_id'],$depot)){
                $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['depot_id'] = $v['depot_id'];
                $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['depot_name'] = $v['depot_name'];
                $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['linkman'] = $v['linkman'];
                $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['linkman_mobile'] = $v['linkman_mobile'];
                $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['address'] = $v['region_name'].$v['address'];
                array_push($depot,$v['depot_id']);
                // } 
                $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['goods_list'][$k]['goods_id'] = $v['goods_id'];
                $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['goods_list'][$k]['goods_code'] = $v['goods_code'];
                $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['goods_list'][$k]['goods_name'] = $v['goods_name'];
                $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['goods_list'][$k]['category_name'] = $v['category_name'];
                $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['goods_list'][$k]['brand_name'] = $v['brand_name'];
                $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['goods_list'][$k]['attribute'] = $v['attribute'];
                $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['goods_list'][$k]['units_name'] = $v['units_name'];
                $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['goods_list'][$k]['number'] = 1;
                array_push($arr,$v['supplier_id']);
            }else{
                if(!in_array($v['depot_id'],$depot)){
                    $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['depot_id'] = $v['depot_id'];
                    $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['depot_name'] = $v['depot_name'];
                    $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['linkman'] = $v['linkman'];
                    $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['linkman_mobile'] = $v['linkman_mobile'];
                    $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['address'] = $v['region_name'].$v['address'];
                    array_push($depot,$v['depot_id']);
                } 
                $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['goods_list'][$k]['goods_id'] = $v['goods_id'];
                $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['goods_list'][$k]['goods_code'] = $v['goods_code'];
                $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['goods_list'][$k]['goods_name'] = $v['goods_name'];
                $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['goods_list'][$k]['category_name'] = $v['category_name'];
                $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['goods_list'][$k]['brand_name'] = $v['brand_name'];
                $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['goods_list'][$k]['attribute'] = $v['attribute'];
                $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['goods_list'][$k]['units_name'] = $v['units_name'];
                $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['goods_list'][$k]['number'] = 1;
                array_push($arr,$v['supplier_id']);
            }
        }
        $list = array_values($data);
        foreach ($list as $k => $v) {
            $list[$k]['addr_list'] = array_values($list[$k]['addr_list']);
        }
        foreach ($list as $k => $v) {
            foreach ($v['addr_list'] as $i => $y) {
                $list[$k]['addr_list'][$i]['goods_list'] = array_values($list[$k]['addr_list'][$i]['goods_list']);
            }
        }
        return $this->outMessage(10000,'保存成功',$list);
    }




    /**
     * 采购申请单添加
     */
    public function api_goods_index_purchaseRequest($msg)
    {   
        if(!is_array($msg['data']) || empty($msg['data'])){
            return $this->outMessage(-1,'参数不能为空');
        }
        $type = isset($msg['type'])?$msg['type']:1;
        $addr_list = [];
        foreach ($msg['data'] as $k => $v) {
            if(!$v['supplier_id'] || !$v['supplier_name']){
                return $this->outMessage(-1,'供应商信息不能为空');
            }
            $addr_list =  isset($v['addr_list'])?$v['addr_list']:'';
        }
        foreach ($addr_list as $k => $v) {
            if(!is_array($v) || empty($v)){
                return $this->outMessage(-1,'详细信息不能为空');
            }
        }
        $this->admin_id = 1;
        $admin = $this->table->find('admin',['id'=>$this->admin_id]);
        foreach ($msg['data'] as $v) {
            $supplier = $this->table->find('supplier',['id'=>$v['supplier_id']]);
            $data = [
                'supplier_id' => $v['supplier_id'],
                'supplier_name' => $v['supplier_name'],
                'supplier_linkman' => $supplier['linkman'],
                'supplier_linkman_mobile' => $supplier['linkman_mobile'],
                'supplier_address' => $supplier['region_name'].$supplier['address'],
                'region_id' => $admin['region_id'],
                'region_name' => $admin['region_name'],
                'itoc_id' => $admin['itoc_id'],
                'itoc_name' => $admin['itoc_name'],
                'create_time' => date('Y-m-d H:i:s',time())
            ];
            $res = $this->table->insert('goods_request',$data);
            if(!$res){
                return $this->outMessage(-1,'提交失败');
            }
            $request_id = $res;
            foreach ($v['addr_list'] as $a) {
                $sum_number = 0;
                $sum_money = 0;
                foreach ($a['goods_list'] as $b) {
                    $goods_info = $this->table->find('goods',['id'=>$b['goods_id']]);
                    $sum_number += $b['number'];
                    $sum_money += $b['number']*$goods_info['price'];
                }
                if($type == 1){
                    $order_no = $this->common->createOutTradeNo('G');
                }else{
                    $order_no = $this->common->createOutTradeNo('HQ');
                }
                $addr = [
                    'order_no' => $order_no,
                    'request_id' => $request_id,
                    'link_order_no' => $type == 2?$a['order_no']:'',
                    'depot_id' => $a['depot_id'],
                    'depot_name' => $a['depot_name'],
                    'address' => $a['address'],
                    'linkman' => $a['linkman'],
                    'linkman_mobile' => $a['linkman_mobile'],
                    'sum_number' => $sum_number,
                    'sum_money' => $sum_money,
                    'remark' => $v['remark'],
                    'type' => $type,
                    'create_time' => date('Y-m-d H:i:s',time())
                ];
                $res = $this->table->insert('goods_request_addr',$addr);
                if(!$res){
                    return $this->outMessage(-1,'提交失败');
                }
                //创建入库单
                if($type == 2){
                    $purchase_data = [
                        'order_no' => $addr['order_no'],
                        'depot_id' => $a['depot_id'],
                        'depot_name' => $a['depot_name'],
                        'itoc_id' => $admin['itoc_id'],
                        'itoc_name' => $admin['itoc_name'],
                        'supplier_id' => $v['supplier_id'],
                        'supplier_name' => $v['supplier_name'],
                        'number' => $sum_number,
                        'remark' => $v['remark'],
                    ]; 
                    $res = $this->table->insert('depot_purchase',$purchase_data);
                    if(!$res){
                        return $this->outMessage(-1,'提交失败');
                    }
                }
                foreach ($a['goods_list'] as $i) {
                    $goods = $this->table->find('goods',['id'=>$i['goods_id']]);
                    $g_data = [
                        'order_no' => $addr['order_no'],
                        'goods_id' => $i['goods_id'],
                        'goods_code' => $goods['code'],
                        'goods_name' => $goods['name'],
                        'brand_id' => $goods['brand_id'],
                        'brand_name' => $goods['brand_name'],
                        'category_id' => $goods['category_id_2'],
                        'category_name' => $goods['category_name_2'],
                        'attribute' => $goods['attribute'],
                        'price' => $goods['price'],
                        'units' => $goods['units_name'],
                        'number' => $i['number'],
                        'create_time' => date('Y-m-d H:i:s',time())
                    ];
                    $res = $this->table->insert('goods_request_list',$g_data);
                    if(!$res){
                        return $this->outMessage(-1,'提交失败');
                    }
                    //插入入库单
                    if($type == 2){
                        unset($g_data['units']);
                        $g_data['units_name'] = $goods['units_name'];
                        $res = $this->table->insert('depot_goods',$g_data);
                        if(!$res){
                            return $this->outMessage(-1,'提交失败');
                        }
                    }
                }
            }
        }
        return $this->outMessage(10000,'提交成功');
    }



    /**
     * 采购申请单手动添加
     */
    public function api_goods_index_addGoodsRequest($msg)
    {   
        $info = [
            'depot_id' =>  isset($msg['data']['depot_id'])?$msg['data']['depot_id']:'',
            'supplier_id' => isset($msg['data']['supplier_id'])?$msg['data']['supplier_id']:''
        ];
        $goods_list = isset($msg['data']['goods_list'])?$msg['data']['goods_list']:'';
        if(!$info['depot_id'] || !$info['supplier_id'] || !$goods_list){
            return $this->outMessage(-1,'参数不能为空');
        }
        $info['remark'] = isset($msg['data']['remark'])?$msg['data']['remark']:'';
        if(!empty($info['remark'])){
            if(!$this->validate->checkName($info['remark'],2,100)){
                return $this->outMessage(-1,'请输入正确的备注');
            }
        }
        $this->admin_id = 1;
        $admin = $this->table->find('admin',['id'=>$this->admin_id]);
        $supplier = $this->table->find('supplier',['id'=>$info['supplier_id']]);
        $depot = $this->table->find('depot',['id'=>$info['depot_id']]);
        $data = [
            'supplier_id' => $info['supplier_id'],
            'supplier_name' => $supplier['name'],
            'supplier_linkman' => $supplier['linkman'],
            'supplier_linkman_mobile' => $supplier['linkman_mobile'],
            'supplier_address' => $supplier['region_name'].$supplier['address'],
            'region_id' => $admin['region_id'],
            'region_name' => $admin['region_name'],
            'itoc_id' => $admin['itoc_id'],
            'itoc_name' => $admin['itoc_name'],
            'create_time' => date('Y-m-d H:i:s',time())
        ];
        $res = $this->table->insert('goods_request',$data);
        if(!$res){
            return $this->outMessage(-1,'提交失败');
        }
        $order_no = $this->common->createOutTradeNo('G');
        $sum_number = 0;
        $sum_money = 0;
        foreach ($goods_list as $v) {
            $goods_info = $this->table->find('goods',['id'=>$v['goods_id']]);
            $sum_number += $v['number'];
            $sum_money += $v['number']*$goods_info['price'];
        }
        $addr = [
            'order_no' => $order_no,
            'request_id' => $res,
            'link_order_no' => '',
            'depot_id' => $info['depot_id'],
            'depot_name' => $depot['name'],
            'address' => $depot['address'],
            'linkman' => $depot['linkman'],
            'linkman_mobile' => $depot['linkman_mobile'],
            'sum_number' => $sum_number,
            'sum_money' => $sum_money,
            'remark' => $info['remark'],
            'create_time' => date('Y-m-d H:i:s',time())
        ];
        $res = $this->table->insert('goods_request_addr',$addr);
        if(!$res){
            return $this->outMessage(-1,'提交失败');
        }
        foreach ($goods_list as $v) {
            $goods = $this->table->find('goods',['id'=>$v['goods_id']]);
            $g_data = [
                'order_no' => $order_no,
                'goods_id' => $v['goods_id'],
                'goods_code' => $goods['code'],
                'goods_name' => $goods['name'],
                'brand_id' => $goods['brand_id'],
                'brand_name' => $goods['brand_name'],
                'category_id' => $goods['category_id_2'],
                'category_name' => $goods['category_name_2'],
                'attribute' => $goods['attribute'],
                'price' => $goods['price'],
                'units' => $goods['units_name'],
                'number' => $v['number'],
                'create_time' => date('Y-m-d H:i:s',time())
            ];
            $res = $this->table->insert('goods_request_list',$g_data);
            if(!$res){
                return $this->outMessage(-1,'提交失败');
            }
        }
        return $this->outMessage(10000,'提交成功');
    }



    /**
     * 获取商品采购申请单列表
     */
    public function api_goods_index_purchaseRequestList($msg)
    {   
        global $db;
        $info = [
            'page' =>  isset($msg['data']['page'])?$msg['data']['page']:1,
            'rows' => isset($msg['data']['rows'])?$msg['data']['rows']:15,
            'order_no' => isset($msg['data']['order_no'])?$msg['data']['order_no']:'',
            'status' => isset($msg['data']['status'])?$msg['data']['status']:'',
            'itoc_name' => isset($msg['data']['itoc_name'])?$msg['data']['itoc_name']:'',
            'depot_name' => isset($msg['data']['depot_name'])?$msg['data']['depot_name']:'',
            'supplier_name' => isset($msg['data']['supplier_name'])?$msg['data']['supplier_name']:'',
            'type' => isset($msg['data']['type'])?$msg['data']['type']:1
        ];
        $limit = ($info['page']-1)*$info['rows'].','.$info['rows'];
        $where = "a.is_delete = 0 AND type = ".$info['type'];
        if(!empty($info['status'])){
            $where .= " AND a.status =".$info['status'];
        }
        if(!empty($info['order_no'])){
            $where .= " AND a.order_no LIKE '%".$info['order_no']."%'";
        }
        if(!empty($info['itoc_name'])){
            $where .= " AND b.itoc_name LIKE '%".$info['itoc_name']."%'";
        }
        if(!empty($info['depot_name'])){
            $where .= " AND a.depot_name LIKE '%".$info['depot_name']."%'";
        }
        if(!empty($info['supplier_name'])){
            $where .= " AND b.supplier_name LIKE '%".$info['supplier_name']."%'";
        }
        $list = $db->query("SELECT a.id,a.order_no,a.status,a.create_time,a.buy_time,b.itoc_name,a.depot_name,b.supplier_name,a.sum_number,a.actual_number,a.sum_money,a.order_number,a.logistics_time,a.linkman,a.linkman_mobile,a.address,b.supplier_linkman,b.supplier_linkman_mobile,b.supplier_address,a.remark,a.check_remark FROM  goods_request_addr as a left join goods_request as b on a.request_id = b.id where ".$where ." GROUP BY a.order_no order by a.id  desc limit ".$limit); 
        $count =  $db->query("SELECT count(*) as count FROM  goods_request_addr as a left join goods_request as b on a.request_id = b.id where ".$where);
        return $this->outMessage(10000,'获取成功',$list,'success',$count[0]['count']);
    }




    /**
     * 获取商品采购申请单详情
     */
    public function api_goods_index_purchaseRequestDetails($msg)
    {   
        $order_no = isset($msg['data']['order_no'])?$msg['data']['order_no']:'';
        $list = $this->table->select('goods_request_list',['order_no'=>$order_no]);
        return $this->outMessage(10000,'获取成功',$list);
    }




    /**
     * 商品采购申请单运营中心审核
     */
    public function api_goods_index_purchaseRequestCheck($msg)
    {   
        $info = [
            'order_no' =>  isset($msg['data']['order_no'])?$msg['data']['order_no']:'',
            'status' => isset($msg['data']['status'])?$msg['data']['status']:'',
        ];
        foreach ($info as $v) {
            if(empty($v)){
                return $this->outMessage(-1,'参数不能为空');
            }
        }
        $info['check_remark'] = isset($msg['data']['check_remark'])?$msg['data']['check_remark']:'';
        if(!empty($info['check_remark'])){
            if(!$this->validate->checkName($info['check_remark'],2,100)){
                return $this->outMessage(-1,'请输入正确的备注');
            }
        }
        $this->admin_id = 1;
        $admin = $this->table->find('admin',['id'=>$this->admin_id]);
        $data = [
            'status' => $info['status'],
            'check_remark' => $info['check_remark'],
            'check_admin_id' => $this->admin_id,
            'check_nickname' => $admin['nickname'],
            'check_time' => date('Y-m-d H:i:s',time())
        ];
        if($info['status'] == 3){
            $data['buy_time'] = date('Y-m-d H:i:s',time());
        }
        $res = $this->table->update('goods_request_addr',['order_no'=>$info['order_no']],$data);
        if(!$res){
            return $this->outMessage(-1,'审核失败');
        }
        if($info['status'] == 3){
            $link = $this->table->find('goods_request_addr',['order_no'=>$info['order_no']]);
            $res = $this->table->update('goods_request_addr',['order_no'=>$link['link_order_no']],['status' => 3,'buy_time'=>date('Y-m-d H:i:s',time())]);
            if(!$res){
                return $this->outMessage(-1,'审核失败');
            }
        }
        return $this->outMessage(10000,'审核成功');
    }



    /**
     * 生成总部采购单
     */
    public function api_goods_index_createPurchase($msg)
    {   
        global $db;
        $ids = isset($msg['data']['ids'])?$msg['data']['ids']:'';
        if(empty($ids)){
            return $this->outMessage(-1,'id不能为空');
        }
        $list = $db->query("SELECT a.id,a.order_no,a.status,a.create_time,a.buy_time,b.itoc_name,a.depot_name,b.supplier_name,a.sum_number,a.actual_number,a.sum_money,a.order_number,a.logistics_time,a.linkman,a.linkman_mobile,a.address,b.supplier_linkman,b.supplier_linkman_mobile,b.supplier_address,b.supplier_id,a.depot_id,c.goods_id,c.goods_code,c.goods_name,c.brand_name,c.category_name,c.attribute,c.units,c.price as units_name,c.number,c.actual_number,c.price FROM  goods_request_addr as a left join goods_request as b on a.request_id = b.id left join goods_request_list as c on a.order_no = c.order_no where a.id in (".$ids .") "); 
        $arr = [];
        $data = [];
        $depot = [];
        foreach ($list as $k => $v) {
            if(!in_array($v['supplier_id'],$arr)){
                $data[$v['supplier_id']]['supplier_id'] = $v['supplier_id'];
                $data[$v['supplier_id']]['supplier_name'] = $v['supplier_name'];
                // if(!in_array($v['depot_id'],$depot)){
                $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['order_no'] = $v['order_no'];
                $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['depot_id'] = $v['depot_id'];
                $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['depot_name'] = $v['depot_name'];
                $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['linkman'] = $v['linkman'];
                $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['linkman_mobile'] = $v['linkman_mobile'];
                $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['address'] = $v['address'];
                $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['itoc_name'] = $v['itoc_name'];
                array_push($depot,$v['depot_id']);
                // } 
                $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['goods_list'][$k]['goods_id'] = $v['goods_id'];
                $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['goods_list'][$k]['goods_code'] = $v['goods_code'];
                $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['goods_list'][$k]['goods_name'] = $v['goods_name'];
                $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['goods_list'][$k]['category_name'] = $v['category_name'];
                $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['goods_list'][$k]['brand_name'] = $v['brand_name'];
                $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['goods_list'][$k]['attribute'] = $v['attribute'];
                $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['goods_list'][$k]['units_name'] = $v['units_name'];
                $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['goods_list'][$k]['number'] = $v['number'];
                $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['goods_list'][$k]['price'] = $v['price'];
                array_push($arr,$v['supplier_id']);
            }else{
                if(!in_array($v['depot_id'],$depot)){
                    $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['order_no'] = $v['order_no'];
                    $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['depot_id'] = $v['depot_id'];
                    $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['depot_name'] = $v['depot_name'];
                    $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['linkman'] = $v['linkman'];
                    $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['linkman_mobile'] = $v['linkman_mobile'];
                    $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['address'] = $v['address'];
                    $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['depot_name'] = $v['depot_name'];
                    array_push($depot,$v['depot_id']);
                } 
                $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['goods_list'][$k]['goods_id'] = $v['goods_id'];
                $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['goods_list'][$k]['goods_code'] = $v['goods_code'];
                $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['goods_list'][$k]['goods_name'] = $v['goods_name'];
                $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['goods_list'][$k]['category_name'] = $v['category_name'];
                $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['goods_list'][$k]['brand_name'] = $v['brand_name'];
                $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['goods_list'][$k]['attribute'] = $v['attribute'];
                $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['goods_list'][$k]['units_name'] = $v['units_name'];
                $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['goods_list'][$k]['number'] = $v['number'];
                $data[$v['supplier_id']]['addr_list'][$v['depot_id']]['goods_list'][$k]['price'] = $v['price'];
                array_push($arr,$v['supplier_id']);
            }
        }
        $list = array_values($data);
        foreach ($list as $k => $v) {
            $list[$k]['addr_list'] = array_values($list[$k]['addr_list']);
        }
        foreach ($list as $k => $v) {
            foreach ($v['addr_list'] as $i => $y) {
                $list[$k]['addr_list'][$i]['goods_list'] = array_values($list[$k]['addr_list'][$i]['goods_list']);
            }
        }
        return $this->outMessage(10000,'保存成功',$list);
    }

 
    /**
     * 采购单发货/填写物流信息
     */
    public function api_goods_index_purchaseRequestSend($msg)
    {
        $order_no = isset($msg['data']['order_no'])?$msg['data']['order_no']:'';
        $order_number = isset($msg['data']['order_number'])?$msg['data']['order_number']:'';
        if(empty($order_no)){
            return $this->outMessage(-1,'订单号不能为空');
        }
        if(empty($order_number)){
            return $this->outMessage(-1,'物流单号不能为空');
        }
        $info = $this->table->find('goods_request_addr',['order_no'=>$order_no]);
        if($info['status'] != 3){
            return $this->outMessage(-1,'该订单状态暂不可发货');
        }
        $data = [
            'status' => 4,
            'order_number' => $order_number,
            'logistics_time' => date('Y-m-d H:i:s',time())
        ];
        $res = $this->table->update('goods_request_addr',['order_no'=>$order_no],$data);
        if(!$res){
            return $this->outMessage(-1,'发货失败');
        }
        //申请采购单修改状态
        $res = $this->table->update('goods_request_addr',['order_no'=>$info['link_order_no']],$data);
        if(!$res){
            return $this->outMessage(-1,'发货失败');
        }
        $this->table->update('depot_purchase',['order_no'=>$info['link_order_no']],['status'=>2]);
        return $this->outMessage(10000,'发货成功');
    }



    /**
     * 商品销售量统计
     *
     * @param type = 1 商品销售额统计
     */
    public function api_goods_index_salesStat($msg)
    {
        global $db;
        $info = [
            'page' => isset($msg['data']['page'])?$msg['data']['page']:1,
            'rows' => isset($msg['data']['rows'])?$msg['data']['rows']:15,
            'date' => isset($msg['data']['date'])?$msg['data']['date']:1,
            'category_id' => isset($msg['data']['category_id'])?$msg['data']['category_id']:'',
            'goods_name' => isset($msg['data']['goods_name'])?$msg['data']['goods_name']:'',
            'itoc_id' => isset($msg['data']['itoc_id'])?$msg['data']['itoc_id']:'',
            'admin_region_id' => isset($msg['data']['admin_region_id'])?$msg['data']['admin_region_id']:'',
            'type' => isset($msg['data']['type'])?$msg['data']['type']:'',
        ];
        if(empty($info['date']) || !is_numeric($info['date']) || $info['date'] < 1 || $info['date'] > 4){
            return $this->outMessage(-1,'时间筛选错误');
        }
        $limit = ($info['page']-1)*$info['rows'].','.$info['rows'];
        $where = " b.order_status = 2";
        if(!empty($info['category_id'])){
            $where .= " AND c.category_id_2 in (".$info['category_id'].") ";
        }
        if(!empty($info['itoc_id'])){
            $where .= " AND b.itoc_id =".$info['itoc_id'];
        }
        if(!empty($info['goods_name'])){
            $where .= " AND a.goods_name LIKE '%".$info['goods_name']."%'";
        }
        //如果存在大区筛选
        if(!empty($info['admin_region_id'])){
            $region_id = $db->query("SELECT region_ids FROM admin_region WHERE id in (".$info['admin_region_id'].")");
            $ids = '';
            foreach ($region_id as $k => $v) {
                if(empty($ids)){
                    $ids .= $v['region_ids'];
                }else{
                    $ids .= ','.$v['region_ids'];
                }
            }
            $region_ids = $this->table->getRegionIds($ids);
            if($region_ids != 'all' && !empty($region_ids)){
                $where .= " AND b.region_id in (".$region_ids.") ";
            }
        }
        $time = $this->common->getTime($info['date'],'',7);
        $where .= " AND a.create_time > '".$time['start'][5]."'";     
        $list = $db->query("SELECT a.goods_id,a.goods_code,a.goods_name FROM order_goods as a left join `order` as b on a.order_no = b.order_no left join goods as c on a.goods_id = c.id WHERE ".$where." GROUP BY a.goods_id limit ".$limit);
        $field = "number";
        if($info['type'] == 1){
            $field = "sum_price";
        }
        if(!empty($list)){
            foreach ($list as $k => $v) {       
                for($i=0;$i<7;$i++){
                    $data = $db->query("SELECT SUM($field) as count FROM order_goods as a LEFT JOIN `order` as b on a.order_no = b.order_no WHERE a.goods_id = ".$v['goods_id']." AND a.create_time > '".$time['start'][$i]."' AND a.create_time < '".$time['stop'][$i]."'");
                    $list[$k][$i] = !empty($data[0]['count'])?$data[0]['count']:0;
                }
                $list[$k]['date'] = $info['date'];  
            }
        }
        $count = $db->query("SELECT a.goods_id,a.goods_code,a.goods_name FROM order_goods as a left join `order` as b on a.order_no = b.order_no left join goods as c on a.goods_id = c.id WHERE ".$where." GROUP BY a.goods_id");
        return $this->outMessage(10000,'获取成功',$list,'success',count($count));  
    }



    /**
     * 商品采购统计
     */
    public function api_goods_index_goodsProcurementStat($msg)
    {
        global $db;
        $info = [
            'page' => isset($msg['data']['page'])?$msg['data']['page']:1,
            'rows' => isset($msg['data']['rows'])?$msg['data']['rows']:15,
            'date' => isset($msg['data']['date'])?$msg['data']['date']:1,
            'supplier_name' => isset($msg['data']['supplier_name'])?$msg['data']['supplier_name']:'',
            'goods_name' => isset($msg['data']['goods_name'])?$msg['data']['goods_name']:'',
        ];
        if(empty($info['date']) || !is_numeric($info['date']) || $info['date'] < 1 || $info['date'] > 4){
            return $this->outMessage(-1,'时间筛选错误');
        }
        $limit = ($info['page']-1)*$info['rows'].','.$info['rows'];
        $where = " a.status = 5";
        if(!empty($info['goods_name'])){
            $where .= " AND b.goods_name LIKE '%".$info['goods_name']."%'";
        }
        if(!empty($info['supplier_name'])){
            $where .= " AND c.supplier_name LIKE '%".$info['supplier_name']."%'";
        }
        $group = '%Y-%m';
        if($info['date'] == 4){
            $group = '%Y';
        }
        if($info['date'] == 1 || $info['date'] ==4){
            $list = $db->query("SELECT date_format(b.create_time,'".$group."') time,c.supplier_name,b.goods_code,b.goods_name,SUM(b.actual_number) as number,b.price FROM goods_request_addr as a left join goods_request_list as b on a.order_no = b.order_no left join goods_request as c on a.request_id = c.id WHERE ".$where." GROUP BY b.goods_id,c.supplier_id,date_format(b.create_time,'".$group."') order by b.create_time limit ".$limit);
            $count = $db->query("SELECT date_format(b.create_time,'".$group."') time,c.supplier_name,b.goods_code,b.goods_name,SUM(b.actual_number) as number,b.price FROM goods_request_addr as a left join goods_request_list as b on a.order_no = b.order_no left join goods_request as c on a.request_id = c.id WHERE ".$where." GROUP BY b.goods_id,c.supplier_id,date_format(b.create_time,'".$group."') ");
        }
        if($info['date'] == 3){
            $list = $db->query("SELECT concat(date_format(b.create_time, '%Y'),FLOOR((date_format(b.create_time, '%m')+2)/3)) as time,c.supplier_name,b.goods_code,b.goods_name,SUM(b.actual_number) as number,b.price FROM goods_request_addr as a left join goods_request_list as b on a.order_no = b.order_no left join goods_request as c on a.request_id = c.id WHERE ".$where." GROUP BY b.goods_id,c.supplier_id,concat(date_format(b.create_time, '%Y'),FLOOR((date_format(b.create_time, '%m')+2)/3)) order by b.create_time limit ".$limit);
            $count = $db->query("SELECT concat(date_format(b.create_time, '%Y'),FLOOR((date_format(b.create_time, '%m')+2)/3)) as time,c.supplier_name,b.goods_code,b.goods_name,SUM(b.actual_number) as number,b.price FROM goods_request_addr as a left join goods_request_list as b on a.order_no = b.order_no left join goods_request as c on a.request_id = c.id WHERE ".$where." GROUP BY b.goods_id,c.supplier_id,concat(date_format(b.create_time, '%Y'),FLOOR((date_format(b.create_time, '%m')+2)/3))");
            if(!empty($list)){
                foreach ($list as $k => $v) {
                    $list[$k]['time'] = substr($v['time'],0,4).'-Q'.substr($v['time'],4);
                }
            }
        }
        if(!empty($list)){
            foreach ($list as $k => $v) {
                $list[$k]['sum_money'] = $v['number']*$v['price'];
            }
        }
        return $this->outMessage(10000,'获取成功',$list,'success',count($count));  
    }

}
